﻿/*
Copyright (C) 2012 Laurent Tardif (laurent.tardif@persistentsas.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TechTalk.SpecFlow;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using org.codeplex.TestUtils;

namespace IQToolsFunctionalTests.src.Stories.Diff2HTML
{
    [Binding]
    public class StepDefinition1
    {
        String generatedFile = null;
        String xmlinputfile = null;

        [Given(@"I have provided a xml file named (.*) with (.*) differences")]
        public void GivenIHaveProvidedAXmlFileWithSoftwareDifferences(string filename, string elements)
        {
            String dir = Directory.GetCurrentDirectory();
            xmlinputfile = Path.Combine(dir, filename);
            Console.WriteLine("Will work with configuration file " + xmlinputfile);
            Assert.IsTrue(File.Exists(xmlinputfile), "the file " + xmlinputfile + " should exist");
        }

        [Given(@"I have given an diffHTML outputfile named (.*)")]
        public void GivenIHaveGivenAnOutputfileNamed(string filename)
        {
            String dir = Directory.GetCurrentDirectory();
            generatedFile = Path.Combine(dir, filename);
            Console.WriteLine("Will work with result file " + generatedFile);
            Assert.IsFalse(File.Exists(generatedFile), "the file " + generatedFile + " should not exist");
        }


        [Then(@"the diffHTML file should contain (.*) in the content")]
        public void WhenTheHTMLFileShouldContainsString(string sectionName)
        {
            Console.WriteLine("Will work with result file " + generatedFile);
            Assert.IsTrue(File.Exists(generatedFile), "the file " + generatedFile + " should exist");
            String content = File.ReadAllText(generatedFile);
            Boolean check = StringUtils.CheckContains(content, sectionName);
            Assert.IsTrue(check, "File should contain " + sectionName);
        }


        [When(@"I run the DIFF2HTML script")]
        public void WhenIRunTheDIFF2HTMLScript()
        {
            Console.WriteLine("start success");
            Console.WriteLine("Diff2HTML.ps1 -filename  " + xmlinputfile + " -outputfilename " + generatedFile + " ");
            //IN 32 mode on 64bit box, as the xml nodes are not odered in the same order
            int errorCode = ShellUtils.ExecutePowershellScript32("Diff2HTML.ps1", @"-filename " + xmlinputfile + " -outputfilename " + generatedFile + " ", 50000);

            Assert.AreEqual(0, errorCode, "The return code should be = 0 as all parameters are valid (directory)");
            Console.WriteLine("run success");
        }
    }
}
